﻿using Commons.Helpers;
using Commons.Responses;
using DomainCommons.Models;
using EntityModels.E02.Identities;
using IRepositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using ORM;

namespace Repositories
{
    public class UserRepository : BaseRepository<Guid, AppUser>, IUserRepository
    {
        private readonly UserManager<AppUser> userManager;
        public UserRepository(PSPDbContext context,UserManager<AppUser> userManager) : base(context) { this.userManager = userManager; }

        public async Task<MessageResponse> UpdateAvatar(AppUser user)
        {
            var existingEntity = await Context.Users.FirstOrDefaultAsync(x => x.Id.Equals(user.Id));
            try
            {
                Context.Entry(existingEntity).CurrentValues.SetValues(user);
                await Context.SaveChangesAsync();
                return new MessageResponse ("头像修改成功!");
            }
            catch (Exception ex)
            {
                throw new CustomException("头像修改失败!");
            }
        }
        //public override async Task<MessageResponse> UpdateAsync(AppUser ddo)
        //{
        //    try
        //    {
        //        var res = await userManager.UpdateAsync(ddo);
        //        return await base.UpdateAsync(ddo);
        //    }catch(Exception e)
        //    {
        //        throw new CustomException("更新失败!");
        //    }
            
        //}

    }
}
